import 'package:flutter/material.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x00_aleret_item.dart';

class AlertDialogPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Alerts"),
      ),
      body: Padding(
        padding: EdgeInsetsDirectional.only(
          top: 18,
          bottom: 18
        ),
        child: ListView(
          children: <Widget>[
            AlertItem("Alert Dialog", () => _showAlertDialog(context)),
            AlertItem("Alert Dialog 1", () => _showAlertDialog_1(context)),
            AlertItem("Alert Dialog 2", () => _showAlertDialog_2(context)),
          ],
        ),
      )
    );
  }

  ///
  /// AlertDialog
  /// 下面我们主要介绍一下Material库中的AlertDialog组件，它的构造函数定义如下：
  /// 
  /// ```
  /// const AlertDialog({
  ///   Key key,
  ///   this.title, //对话框标题组件
  ///   this.titlePadding, // 标题填充
  ///   this.titleTextStyle, //标题文本样式
  ///   this.content, // 对话框内容组件
  ///   this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0), //内容的填充
  ///   this.contentTextStyle,// 内容文本样式
  ///   this.actions, // 对话框操作按钮组
  ///   this.backgroundColor, // 对话框背景色
  ///   this.elevation,// 对话框的阴影
  ///   this.semanticLabel, //对话框语义化标签(用于读屏软件)
  ///   this.shape, // 对话框外形
  /// })
  /// ```
  /// 

}

////////////////////////////////////////////////////////////////////////
void _showAlertDialog(BuildContext context) async {
  bool delete = await _showAlertDialogAction(context);
  if (delete) {
    print("Deleted");
  } else {
    print("Cancel");
  }
}
Future <bool> _showAlertDialogAction(BuildContext context) {
  return showDialog<bool>(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: Text("Tips"),
        content: Text("Are you sure delete current files?"),
        actions: <Widget>[
          FlatButton(
            child: Text("Cancel"),
            /// 需要注意的是我们是通过Navigator.of(context).pop(…)方法来关闭对话框的，
            /// 这和路由返回的方式是一致的，并且都可以返回一个结果数据
            onPressed: () => Navigator.of(context).pop(false),
          ),
          FlatButton(
            child: Text("Delete"),
            onPressed: () => Navigator.of(context).pop(true),
          )
        ],
      );
    },
  );
}
////////////////////////////////////////////////////////////////////////
void _showAlertDialog_1(BuildContext context) async {
  int index = await _alert1_x(
    context,
    title: "Alert Title",
    content: "Alert Content",
    actions: ["Action1", "Action2", "Action3"]
  );
  print(index);
}
void _showAlertDialog_2(BuildContext context) async {
  int index = await _alert1_x(
    context,
    content: "Alert Content",
  );
  print(index);
}
Future <int> _alert1_x( BuildContext context, 
  { String title,
    @required String content,
    List<String> actions = const ["Confirm"]
  }) {
  return showDialog<int>(
    context: context,
    builder: (ctx) {
      return AlertDialog(
        title: title == null ? null : Text(title),
        content: Text(content),
        actions: actions.map((value) {
          return FlatButton(
            child: Text(value),
            onPressed: () {
              Navigator.of(context).pop(actions.indexOf(value));
            },
          );
        }).toList(),
      );
    }
  );
}

void alert(BuildContext context, { String title, @required String content }) {
  _alert1_x(context, title: title, content: content);
}
